package es.android.supermanager;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.util.ByteArrayBuffer;

import es.android.supermanager.R;

import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.*;


public class Mercado extends ListActivity {
	private String resultado;
	private TextView tv;
	private InsertarDatos insertar;
	private Jugador [] bases, aleros, pivots;
	private ArrayAdapter adapter;
	private ListActivity lista;
    static Cursor cursor;
	
    private static String[] cadenas;
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		lista = this;

		
		new Handler();
		insertar = new InsertarDatos();

		bases = insertar.sacarDatos("bases");
		aleros = insertar.sacarDatos("aleros");
		pivots = insertar.sacarDatos("pivots");
		
				
		  Jugador jug;
		  int i=0;
		  while ((jug=bases[i])!=null) {
			  i++;
		  }
		  
		  String cadenas[] = new String[i];
		  
		  i=0;
		  while ((jug=bases[i])!=null) {
			  cadenas[i] = jug.toString();
			  i++;
		  }

		
		  super.onCreate(savedInstanceState);

		  mapear();

	}
	
	private void mapear () {
		
		ArrayList<Jugador> listaBases = new ArrayList(Arrays.asList(bases));
		
		List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
		
		int i=0;
		
		while (bases[i]!=null) {
			HashMap<String,String> map = new HashMap<String,String>();
			map.put("nombre", bases[i].getNombre());
			map.put("equipo", bases[i].getEquipo());
			map.put("media", String.valueOf(bases[i].getMedia()));
			map.put("precio", String.valueOf(bases[i].getPrecio()));
			map.put("tiempo", bases[i].getTiempo());
			map.put("val_ultima", String.valueOf(bases[i].getVal_ultima()));
			map.put("val_3ultimas", String.valueOf(bases[i].getVal_3()));
			map.put("sube15", String.valueOf(bases[i].getSube15()));
			map.put("semantiene", String.valueOf(bases[i].getSe_mantiene()));
			map.put("baja15", String.valueOf(bases[i].getBaja15()));
			list.add(map);
			i++;
		}
		
		String[] from = {"nombre", "equipo", "media", "precio", "tiempo", "val_ultima", "val_3ultimas", "sube15", "semantiene", "baja15"};
		
		int[] to = {R.id.nombre, R.id.equipo, R.id.media, R.id.precio, R.id.tiempo, R.id.ultimo, R.id.ultimos3,  R.id.sube15, R.id.semantiene, R.id.baja15};
		
		ListView lv = getListView();

		SimpleAdapter adapter = new SimpleAdapter(this.getApplicationContext(), (List<? extends Map<String, ?>>) list, R.layout.elemento, from, to);
		lv.setAdapter(adapter);
		
	}
	

}
